const http = require('http');

const server = http.createServer((req, res) => {

    let url = req.url;
    let jsonData;
    if (url == '/user/lst') {
        jsonData = {
            "statusCode": 200,
            "message": "查询成功",
            "data": {
                "list": [{
                        "id": 1,
                        "name": "张三",
                        "age": 23,
                        "address": "北京",
                        "phone": "13000000000",
                        "email": "zhangsan@sina.com"
                    },
                    {
                        "id": 2,
                        "name": "李四",
                        "age": 22,
                        "address": "广州",
                        "phone": "13230000000",
                        "email": "lisi@sina.com"
                    },
                    {
                        "id": 3,
                        "name": "王五",
                        "age": 25,
                        "address": "深圳",
                        "phone": "19860000000",
                        "email": "wangwu@163.com"
                    }
                ],
                "total": 3
            }
        }
    } else {
        jsonData = {
            "statusCode": -1,
            "message": "api not found!",
            "data": null,
        }
    }


    // 设置允许跨域请求
    /* Access-Control-Allow-Origin 这个是响应头设置代表允许跨域的域名 * 代表允许任意浏览器进行跨域 */
    // res.setHeader("Access-Control-Allow-Origin", "*");

    // 设置响应头为 json格式数据，编码为  utf-8
    res.setHeader('Content-Type', 'application/json;charset=utf-8');
    res.write(JSON.stringify(jsonData));
    res.end();

});
const port = 8081;

server.listen(port, () => {
    console.log(`server is running at http://localhost:${port}`);
});
